package org.dingyom.market.controller;

import org.dingyom.market.dto.ClueAndActivityDto;
import org.dingyom.market.dto.ClueBusinessDto;
import org.dingyom.market.service.IClueService;
import org.dingyom.market.domain.Clue;
import org.dingyom.market.query.ClueQuery;
import org.dingyom.basics.util.AjaxResult;
import org.dingyom.basics.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clue")
public class ClueController {
    @Autowired
    public IClueService clueService;


    /**
     * 保存和修改公用的
     * @param clue  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Clue clue){
        try {
            if( clue.getId()!=null)
                clueService.modify(clue);
            else
                clueService.add(clue);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
     * 参加活动
     * @param cad  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping("/assoc")
    public AjaxResult association(@RequestBody ClueAndActivityDto cad){
        try {
            clueService.association(cad);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            clueService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Clue clue = clueService.getOne(id);
            return AjaxResult.me().setData(clue);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< Clue> list = clueService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody ClueQuery query)
    {
        try {
            PageList<Clue> pageList = clueService.getPage(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            clueService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }

    /**
     * 分配营销人员
     *
     * @param clue 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/assign")
    public AjaxResult assign(@RequestBody Clue clue)
    {
        try {
            clueService.assign(clue);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }
    /**
     * 转化成商机
     *
     * @param dto 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/business")
    public AjaxResult business(@RequestBody ClueBusinessDto dto)
    {
        try {
            clueService.business(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }
}
